<?php

namespace App\Http\Controllers;

use App\Models\Event;
use Illuminate\Http\Request;

class EventController extends Controller
{
    /**
     * 获取所有事件
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        // 按照创建时间倒序排列，确保最新的演出显示在前面
        $events = Event::orderBy('created_at', 'desc')->get();
        
        // 转换字段名为驼峰命名，以便前端使用
        $formattedEvents = $events->map(function($event) {
            return [
                'id' => $event->id,
                'title' => $event->title,
                'venue' => $event->venue,
                'date' => $event->date,
                'price' => $event->price,
                'description' => $event->description,
                'imageUrl' => $event->image_url, // 转换为驼峰命名
                'city' => $event->city,
                'category' => $event->category,
                'createdAt' => $event->created_at,
                'updatedAt' => $event->updated_at
            ];
        });
        
        return response()->json($formattedEvents);
    }

    /**
     * 获取单个事件
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $event = Event::find($id);
        if ($event) {
            // 转换字段名为驼峰命名
            $formattedEvent = [
                'id' => $event->id,
                'title' => $event->title,
                'venue' => $event->venue,
                'date' => $event->date,
                'price' => $event->price,
                'description' => $event->description,
                'imageUrl' => $event->image_url, // 转换为驼峰命名
                'city' => $event->city,
                'category' => $event->category,
                'createdAt' => $event->created_at,
                'updatedAt' => $event->updated_at
            ];
            return response()->json($formattedEvent);
        } else {
            return response()->json(['error' => 'Event not found'], 404);
        }
    }

    /**
     * 创建新事件
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'venue' => 'required|string|max:255',
            'date' => 'required|date',
            'price' => 'required|numeric',
            'city' => 'required|string|max:50',
            'category' => 'required|string|max:50',
            'description' => 'required|string',
            'image_url' => 'required|string',
        ]);

        $event = Event::create($validated);
        return response()->json($event, 201);
    }

    /**
     * 更新事件
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $event = Event::find($id);
        if (!$event) {
            return response()->json(['error' => 'Event not found'], 404);
        }

        $validated = $request->validate([
            'title' => 'sometimes|required|string|max:255',
            'venue' => 'sometimes|required|string|max:255',
            'date' => 'sometimes|required|date',
            'price' => 'sometimes|required|numeric',
            'city' => 'sometimes|required|string|max:50',
            'category' => 'sometimes|required|string|max:50',
            'description' => 'sometimes|required|string',
            'image_url' => 'sometimes|required|string',
        ]);

        $event->update($validated);
        return response()->json($event);
    }

    /**
     * 删除事件
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $event = Event::find($id);
        if (!$event) {
            return response()->json(['error' => 'Event not found'], 404);
        }

        $event->delete();
        return response()->json(['message' => 'Event deleted successfully']);
    }
}